package com.atguigu.springcloud.payment.controller;


import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.payment.service.IPaymentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hyman
 * @since 2020-07-06
 */
@RestController
@RequestMapping("/payment")
public class PaymentController {
    private static Logger logger = LoggerFactory.getLogger(PaymentController.class);

    @Resource
    private IPaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @Resource
    private DiscoveryClient discoveryClient;

    @GetMapping("/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable String id) {
        return CommonResult.success("当前服务：" + serverPort, paymentService.getById(id));
    }

    @PostMapping("/create")
    public CommonResult<Payment> getPaymentById(@RequestBody Payment payment) {
        payment.setId(null);
        boolean save = paymentService.save(payment);
        if (save) {
            return CommonResult.success("保存成功！,serverPort:" + serverPort);
        } else {
            return CommonResult.failure(444, "保存失败！,serverPort:" + serverPort);
        }
    }

    @GetMapping(value = "/lb")
    public String getPaymentLB(){
        return serverPort;
    }

    @GetMapping(value = "/time-out")
    public String testTimeOut() throws InterruptedException {
        TimeUnit.SECONDS.sleep(2);
        return serverPort;
    }
}

